﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace App
{

    public class BaseStyledPanel : ContainerControl
    {
        #region Members
        private static ToolStripProfessionalRenderer renderer;
        #endregion

        #region Events
        public event EventHandler ThemeChanged;
        #endregion

        #region Ctor
        static BaseStyledPanel()
        {
            renderer = new ToolStripProfessionalRenderer();
        }

        public BaseStyledPanel()
        {
            // Set painting style for better performance.
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
        }
        #endregion

        #region Methods
        protected override void OnSystemColorsChanged(EventArgs e)
        {
            base.OnSystemColorsChanged(e);
            UpdateRenderer();
            Invalidate();
        }

        protected virtual void OnThemeChanged(EventArgs e)
        {
            if (ThemeChanged != null)
                ThemeChanged(this, e);
        }

        private void UpdateRenderer()
        {
            if (!UseThemes)
            {
                renderer.ColorTable.UseSystemColors = true;
            }
            else
            {
                renderer.ColorTable.UseSystemColors = false;
            }
        }
        #endregion

        #region Props
        [Browsable(false)]
        public ToolStripProfessionalRenderer ToolStripRenderer
        {
            get { return renderer; }
        }

        [DefaultValue(true)]
        [Browsable(false)]
        public bool UseThemes
        {
            get
            {
                return VisualStyleRenderer.IsSupported && VisualStyleInformation.IsSupportedByOS && Application.RenderWithVisualStyles;
            }
        }
        #endregion
    }
    public interface ICaptionSupport
    {
        string Caption
        {
            get;
        }
    }

    #region Delegates
    public delegate void CollectionClear();
    public delegate void CollectionChange(int index, object value);
    #endregion

    #region Base class
    public abstract class EventCollection : CollectionBase
    {
        #region Members
        private int _suspendCount;

        [Browsable(false)]
        public event CollectionClear Clearing;

        [Browsable(false)]
        public event CollectionClear Cleared;

        [Browsable(false)]
        public event CollectionChange Inserting;

        [Browsable(false)]
        public event CollectionChange Inserted;

        [Browsable(false)]
        public event CollectionChange Removing;

        [Browsable(false)]
        public event CollectionChange Removed;
        #endregion

        #region Members
        #region Public
        public void SuspendEvents()
        {
            _suspendCount++;
        }

        public void ResumeEvents()
        {
            _suspendCount--;
        }

        // Property
        [Browsable(false)]
        public bool IsSuspended
        {
            get { return (_suspendCount > 0); }
        }
        #endregion

        #region Protected
        protected override void OnClear()
        {
            if (!IsSuspended)
            {
                if (Clearing != null)
                    Clearing();
            }
        }

        protected override void OnClearComplete()
        {
            if (!IsSuspended)
            {
                if (Cleared != null)
                    Cleared();
            }
        }

        protected override void OnInsert(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Inserting != null)
                    Inserting(index, value);
            }
        }

        protected override void OnInsertComplete(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Inserted != null)
                    Inserted(index, value);
            }
        }

        protected override void OnRemove(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Removing != null)
                    Removing(index, value);
            }
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Removed != null)
                    Removed(index, value);
            }
        }

        protected int IndexOf(object value)
        {
            return List.IndexOf(value);
        }
        #endregion
        #endregion

        /// <summary>
        /// Initializes the EventCollection
        /// </summary>
        public EventCollection()
        {
            _suspendCount = 0;
        }
    }
    #endregion

    #region TabStripItemMouseEventArgs
    public class TabStripItemMouseEventArgs : EventArgs
    {
        public TabStripItemMouseEventArgs(TabStripItem item)
        {
            _item = item;
        }

        private TabStripItem _item;
        public TabStripItem Item
        {
            get { return _item; }
            set { _item = value; }
        }
    }
    #endregion

    #region TabStripItemClosingEventArgs
    public class TabStripItemClosingEventArgs : EventArgs
    {
        public TabStripItemClosingEventArgs(TabStripItem item)
        {
            _item = item;
        }

        private bool _cancel = false;
        private TabStripItem _item;

        public TabStripItem Item
        {
            get { return _item; }
            set { _item = value; }
        }

        public bool Cancel
        {
            get { return _cancel; }
            set { _cancel = value; }
        }

    }
    #endregion

    #region TabStripItemChangedEventArgs
    public class TabStripItemChangedEventArgs : EventArgs
    {
        TabStripItem itm;
        TabStripItemChangeTypes changeType;

        public TabStripItemChangedEventArgs(TabStripItem item, TabStripItemChangeTypes type)
        {
            changeType = type;
            itm = item;
        }

        public TabStripItemChangeTypes ChangeType
        {
            get { return changeType; }
        }

        public TabStripItem Item
        {
            get { return itm; }
        }
    }
    #endregion

    public delegate void SelectedItemChangedHandler(TabStripItemMouseEventArgs e);
    public delegate void TabStripItemMouseEnterHandler(TabStripItemMouseEventArgs e);
    public delegate void TabStripItemMouseLeaveHandler(TabStripItemMouseEventArgs e);

    public delegate void TabStripItemChangedHandler(TabStripItemChangedEventArgs e);
    public delegate void TabStripItemClosingHandler(TabStripItemClosingEventArgs e);

    public enum HitTestResult
    {
        CloseButton,
        MenuGlyph,
        TabItem,
        None
    }

    public enum ThemeStyles
    {
        WindowsXP,
        Office2000,
        Office2003
    }

    public enum TabStripItemChangeTypes
    {
        Added,
        Removed,
        Changed,
        SelectionChanged
    }
}
